/*
 * my_order_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../head/gui_callback/my_order_cb.h"
#include "../head/gui_callback/global_cb.h"
#include "../head/common_struct/orderinfo.h"
#include "../head/communicate_with_server/orderinfo_transfer.h"
#include "../head/communicate_with_server/userinfo_transfer.h"

//将订单列表中一项添加到界面的liststore中
void add_orderitem_to_liststore(OrderItem item);
//将订单列表中一项添加到界面的liststore中,退票界面用
void add_orderitem_to_liststore_refund(OrderItem item);

extern GtkBuilder *builder;

enum {
	COL_ORDERID = 0,
	COL_NAME,
	COL_CERTIFICATE_TYPE,
	COL_CERTIFICATE_ID,
	COL_STARTDATE,
	COL_STARTADDRESS,
	COL_ENDADDRESS,
	COL_TRAIN,
	COL_COACH,
	COL_SEAT,
	COL_PRICE,
	COL_IS_REFUND,
	COLS_NUM
};

//处理工具栏中的订单查询的点击事件
void menuitem_search_order_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkComboBox *combobox_search_order_search_date_type;
	GtkEntry *entry_search_order_startdate;
	GtkCalendar *calendar_search_order_startdate;
	GtkEntry *entry_search_order_enddate;
	GtkCalendar *calendar_search_order_enddate;
	GtkEntry *entry_search_order_orderid;
	GtkEntry *entry_search_order_train;
	GtkTreeView *treeview_search_order;

	combobox_search_order_search_date_type =
			GTK_COMBO_BOX(
					gtk_builder_get_object(builder, "combobox_search_order_search_date_type"));
	entry_search_order_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_startdate"));
	calendar_search_order_startdate =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_search_order_startdate"));
	entry_search_order_enddate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_enddate"));
	calendar_search_order_enddate =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_search_order_enddate"));
	entry_search_order_orderid =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_orderid"));
	entry_search_order_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_train"));
	treeview_search_order = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_order"));

	gtk_combo_box_set_active(combobox_search_order_search_date_type, 0);

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char date[ORDERS_DEPARTDATE_LEN];
	memset(date, 0, sizeof(date));
	sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900, tm_ptr->tm_mon + 1,
			tm_ptr->tm_mday);
	gtk_entry_set_text(entry_search_order_startdate, date);
	gtk_calendar_select_month(calendar_search_order_startdate, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_search_order_startdate, tm_ptr->tm_mday);

	gtk_entry_set_text(entry_search_order_enddate, date);
	gtk_calendar_select_month(calendar_search_order_enddate, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_search_order_enddate, tm_ptr->tm_mday);

	gtk_entry_set_text(entry_search_order_orderid, "");
	gtk_entry_set_text(entry_search_order_train, "");

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_order);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	window_show(MY_ORDER_SEARCH_ORDER);
}

//处理订单查询的查询按钮的点击事件
void button_search_order_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkComboBox *combobox_search_order_search_date_type;
	GtkEntry *entry_search_order_startdate;
	GtkEntry *entry_search_order_enddate;
	GtkEntry *entry_search_order_orderid;
	GtkEntry *entry_search_order_train;
	GtkTreeView *treeview_search_order;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	combobox_search_order_search_date_type =
			GTK_COMBO_BOX(
					gtk_builder_get_object(builder, "combobox_search_order_search_date_type"));
	entry_search_order_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_startdate"));
	entry_search_order_enddate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_enddate"));
	entry_search_order_orderid =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_orderid"));
	entry_search_order_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_order_train"));
	treeview_search_order = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_order"));

	gint order_query_type = gtk_combo_box_get_active(
			combobox_search_order_search_date_type);
	const gchar *startdate = gtk_entry_get_text(entry_search_order_startdate);
	const gchar *enddate = gtk_entry_get_text(entry_search_order_enddate);
	const gchar *orderID = gtk_entry_get_text(entry_search_order_orderid);
	const gchar *trainID = gtk_entry_get_text(entry_search_order_train);

	struct orderinfo_search_request request;
	memset(&request, 0, sizeof(request));
	if (order_query_type == ORDER_SEARCH_TYPE_BUY_DATE) {
		request.order_search_type = ORDER_SEARCH_TYPE_BUY_DATE;
	} else if (order_query_type == ORDER_SEARCH_TYPE_DEPART_DATE) {
		request.order_search_type = ORDER_SEARCH_TYPE_DEPART_DATE;
	}

	int startdate_year;
	int startdate_month;
	int startdate_day;
	sscanf(startdate, "%04d-%02d-%02d", &startdate_year, &startdate_month,
			&startdate_day);
	int enddate_year;
	int enddate_month;
	int enddate_day;
	sscanf(enddate, "%04d-%02d-%02d", &enddate_year, &enddate_month,
			&enddate_day);
	bool iserror = false;
	if (startdate_year > enddate_year) {
		iserror = true;
	} else if (startdate_year == enddate_year) {
		if (startdate_month > enddate_month) {
			iserror = true;
		} else if (startdate_month == enddate_month) {
			if (startdate_day > enddate_day) {
				iserror = true;
			}
		}
	}

	if (iserror) {
		gtk_show_warning(global_window, "%s", "起始日期不能晚于结束日期", "输入提醒");
		return;
	}

	struct userinfo *user;
	user = get_user_info();
	strncpy(request.username, user->username, strlen(user->username));
	strncpy(request.startdate, startdate, strlen(startdate));
	strncpy(request.enddate, enddate, strlen(enddate));
	strncpy(request.orderID, orderID, strlen(orderID));
	strncpy(request.trainID, trainID, strlen(trainID));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_order);
	gtk_list_store_clear(GTK_LIST_STORE(model) ); //清空liststore

	OrderList orderlist; //声明订单列表
	InitializeList(&orderlist); //初始化订单列表
	bool search_result = search_order(&request, &orderlist);
	if (search_result) { //找到了订单，逐个显示订单
		Traverse(&orderlist, add_orderitem_to_liststore);
	} else {
		gtk_show_info(global_window, "%s", "未找到相应订单", "信息提示");
	}
	EmptyTheList(&orderlist); //释放订单列表的内存
}

//将订单列表中一项添加到界面的liststore中
void add_orderitem_to_liststore(OrderItem item) {
	GtkTreeView *treeview_search_order;
	GtkTreeModel *model;
	GtkTreeIter iter;

	treeview_search_order = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_order"));
	model = gtk_tree_view_get_model(treeview_search_order);

	gtk_list_store_append(GTK_LIST_STORE(model), &iter);

	gboolean is_refund = false;
	gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_ORDERID, item.orderID,
			COL_NAME, item.name, COL_CERTIFICATE_TYPE, item.creditType,
			COL_CERTIFICATE_ID, item.creditID, COL_STARTDATE, item.departDate,
			COL_STARTADDRESS, item.departStation, COL_ENDADDRESS,
			item.arriveStation, COL_TRAIN, item.trainID, COL_COACH,
			item.compartmentNum, COL_SEAT, item.seatNum, COL_PRICE, item.price,
			COL_IS_REFUND, is_refund, -1);
}

//处理工具栏中的退票的点击事件
void menuitem_ticket_refund_activate_cb(GtkMenuItem *menuitem,
		gpointer user_data) {
	GtkComboBox *combobox_refund_ticket_search_date_type;
	GtkEntry *entry_refund_ticket_startdate;
	GtkCalendar *calendar_refund_ticket_startdate;
	GtkEntry *entry_refund_ticket_enddate;
	GtkCalendar *calendar_refund_ticket_enddate;
	GtkEntry *entry_refund_ticket_orderid;
	GtkEntry *entry_refund_ticket_train;
	GtkTreeView *treeview_refund_ticket;

	combobox_refund_ticket_search_date_type =
			GTK_COMBO_BOX(
					gtk_builder_get_object(builder, "combobox_refund_ticket_search_date_type"));
	entry_refund_ticket_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_startdate"));
	calendar_refund_ticket_startdate =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_refund_ticket_startdate"));
	entry_refund_ticket_enddate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_enddate"));
	calendar_refund_ticket_enddate =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_refund_ticket_enddate"));
	entry_refund_ticket_orderid =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_orderid"));
	entry_refund_ticket_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_train"));
	treeview_refund_ticket = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_refund_ticket"));

	gtk_combo_box_set_active(combobox_refund_ticket_search_date_type, 0);

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char date[ORDERS_DEPARTDATE_LEN];
	memset(date, 0, sizeof(date));
	sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900, tm_ptr->tm_mon + 1,
			tm_ptr->tm_mday);
	gtk_entry_set_text(entry_refund_ticket_startdate, date);
	gtk_calendar_select_month(calendar_refund_ticket_startdate, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_refund_ticket_startdate, tm_ptr->tm_mday);

	gtk_entry_set_text(entry_refund_ticket_enddate, date);
	gtk_calendar_select_month(calendar_refund_ticket_enddate, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_refund_ticket_enddate, tm_ptr->tm_mday);

	gtk_entry_set_text(entry_refund_ticket_orderid, "");
	gtk_entry_set_text(entry_refund_ticket_train, "");

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_refund_ticket);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	window_show(MY_ORDER_REFUND_TICKET);
}

//处理退票时订单查询的查询按钮的点击事件
void button_refund_ticket_search_clicked_cb(GtkButton *button,
		gpointer user_data) {
	GtkComboBox *combobox_refund_ticket_search_date_type;
	GtkEntry *entry_refund_ticket_startdate;
	GtkEntry *entry_refund_ticket_enddate;
	GtkEntry *entry_refund_ticket_orderid;
	GtkEntry *entry_refund_ticket_train;
	GtkTreeView *treeview_refund_ticket;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	combobox_refund_ticket_search_date_type =
			GTK_COMBO_BOX(
					gtk_builder_get_object(builder, "combobox_refund_ticket_search_date_type"));
	entry_refund_ticket_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_startdate"));
	entry_refund_ticket_enddate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_enddate"));
	entry_refund_ticket_orderid =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_orderid"));
	entry_refund_ticket_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_refund_ticket_train"));
	treeview_refund_ticket = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_refund_ticket"));

	gint order_query_type = gtk_combo_box_get_active(
			combobox_refund_ticket_search_date_type);
	const gchar *startdate = gtk_entry_get_text(entry_refund_ticket_startdate);
	const gchar *enddate = gtk_entry_get_text(entry_refund_ticket_enddate);
	const gchar *orderID = gtk_entry_get_text(entry_refund_ticket_orderid);
	const gchar *trainID = gtk_entry_get_text(entry_refund_ticket_train);

	struct orderinfo_search_request request;
	memset(&request, 0, sizeof(request));
	if (order_query_type == ORDER_SEARCH_TYPE_BUY_DATE) {
		request.order_search_type = ORDER_SEARCH_TYPE_BUY_DATE;
	} else if (order_query_type == ORDER_SEARCH_TYPE_DEPART_DATE) {
		request.order_search_type = ORDER_SEARCH_TYPE_DEPART_DATE;
	}

	int startdate_year;
	int startdate_month;
	int startdate_day;
	sscanf(startdate, "%04d-%02d-%02d", &startdate_year, &startdate_month,
			&startdate_day);
	int enddate_year;
	int enddate_month;
	int enddate_day;
	sscanf(enddate, "%04d-%02d-%02d", &enddate_year, &enddate_month,
			&enddate_day);
	bool iserror = false;
	if (startdate_year > enddate_year) {
		iserror = true;
	} else if (startdate_year == enddate_year) {
		if (startdate_month > enddate_month) {
			iserror = true;
		} else if (startdate_month == enddate_month) {
			if (startdate_day > enddate_day) {
				iserror = true;
			}
		}
	}

	if (iserror) {
		gtk_show_warning(global_window, "%s", "起始日期不能晚于结束日期", "输入提醒");
		return;
	}

	struct userinfo *user;
	user = get_user_info();
	strncpy(request.username, user->username, strlen(user->username));
	strncpy(request.startdate, startdate, strlen(startdate));
	strncpy(request.enddate, enddate, strlen(enddate));
	strncpy(request.orderID, orderID, strlen(orderID));
	strncpy(request.trainID, trainID, strlen(trainID));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_refund_ticket);
	gtk_list_store_clear(GTK_LIST_STORE(model) ); //清空liststore

	OrderList orderlist; //声明订单列表
	InitializeList(&orderlist); //初始化订单列表
	bool search_result = search_order(&request, &orderlist);
	if (search_result) { //找到了订单，逐个显示订单
		Traverse(&orderlist, add_orderitem_to_liststore_refund);
	} else {
		gtk_show_info(global_window, "%s", "未找到相应订单", "信息提示");
	}
	EmptyTheList(&orderlist); //释放订单列表的内存
}

//将订单列表中一项添加到界面的liststore中,退票界面用
void add_orderitem_to_liststore_refund(OrderItem item) {
	GtkTreeView *treeview_refund_ticket;
	GtkTreeModel *model;
	GtkTreeIter iter;

	treeview_refund_ticket = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_refund_ticket"));
	model = gtk_tree_view_get_model(treeview_refund_ticket);

	gtk_list_store_append(GTK_LIST_STORE(model), &iter);

	gboolean is_refund = false;
	gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_ORDERID, item.orderID,
			COL_NAME, item.name, COL_CERTIFICATE_TYPE, item.creditType,
			COL_CERTIFICATE_ID, item.creditID, COL_STARTDATE, item.departDate,
			COL_STARTADDRESS, item.departStation, COL_ENDADDRESS,
			item.arriveStation, COL_TRAIN, item.trainID, COL_COACH,
			item.compartmentNum, COL_SEAT, item.seatNum, COL_PRICE, item.price,
			COL_IS_REFUND, is_refund, -1);
}

//处理退票按钮的点击事件
void button_refund_ticket_refund_clicked_cb(GtkButton *button,
		gpointer user_data) {
	GtkWindow *global_window;
	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));

	GtkTreeModel *model = (GtkTreeModel *) user_data;
	GtkTreeIter iter;
	gboolean valid;
	gboolean isrefund;

	OrderList orderlist;
	InitializeList(&orderlist);

	valid = gtk_tree_model_get_iter_first(model, &iter);
	while (valid) {
		gtk_tree_model_get(model, &iter, COL_IS_REFUND, &isrefund, -1);
		if (isrefund) {
			OrderItem item;
			memset(&item, 0, sizeof(item));
			gchar *orderID;
			gchar *trainID;
			gchar *compartmentNum;
			gchar *seatNum;
			gchar *name;
			gchar *creditType;
			gchar *creditID;
			gchar *departDate;
			gchar *departStation;
			gchar *arriveStation;
			gchar *price;

			gtk_tree_model_get(model, &iter, COL_ORDERID, &orderID, COL_NAME,
					&name, COL_CERTIFICATE_TYPE, &creditType,
					COL_CERTIFICATE_ID, &creditID, COL_STARTDATE, &departDate,
					COL_STARTADDRESS, &departStation, COL_ENDADDRESS,
					&arriveStation, COL_TRAIN, &trainID, COL_COACH,
					&compartmentNum, COL_SEAT, &seatNum, COL_PRICE, &price, -1);
			strcpy(item.orderID, orderID);
			strcpy(item.name, name);
			strcpy(item.creditType, creditType);
			strcpy(item.creditID, creditID);
			strcpy(item.departDate, departDate);
			strcpy(item.departStation, departStation);
			strcpy(item.arriveStation, arriveStation);
			strcpy(item.trainID, trainID);
			strcpy(item.compartmentNum, compartmentNum);
			strcpy(item.seatNum, seatNum);
			strcpy(item.price, price);

			AddItem(item, &orderlist);
			g_free(orderID);
			g_free(trainID);
			g_free(compartmentNum);
			g_free(seatNum);
			g_free(name);
			g_free(creditType);
			g_free(creditID);
			g_free(departDate);
			g_free(departStation);
			g_free(arriveStation);
			g_free(price);
		}
		valid = gtk_tree_model_iter_next(model, &iter);
	}

	unsigned int count = ListItemCount(&orderlist);
	unsigned int len = (ORDERS_ORDERID_LEN + 1) * count;
	len += strlen("是否退订订单号如下的订单:\n");

	char message[len];
	if (count == 0) {
		gtk_show_warning(global_window, "%s", "您未选择订单", "退票提醒");
		return;
	}

	memset(message, 0, sizeof(message));
	strcat(message, "是否退订订单号如下的订单:\n");
	OrderNode *node = orderlist;
	while (node != NULL ) {
		OrderItem item = node->item;
		strcat(message, item.orderID);
		strcat(message, "\n");
		node = node->next;
	}

	bool response = gtk_show_question(global_window, "%s", message, "退票提醒");
	if (response) {
		struct orderinfo_refund_request request;
		memset(&request, 0, sizeof(request));

		struct userinfo *user;
		user = get_user_info();
		strncpy(request.username, user->username, strlen(user->username));

		bool refund_result = refund_order(&request, &orderlist);
		if (refund_result) {
			gtk_show_info(global_window, "%s", "退订成功", "退票提示");
			valid = gtk_tree_model_get_iter_first(model, &iter); //清楚退订的订单
			while (valid) {
				gtk_tree_model_get(model, &iter, COL_IS_REFUND, &isrefund, -1);
				if (isrefund) {
					gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
					valid = gtk_list_store_iter_is_valid(GTK_LIST_STORE(model),
							&iter);
				} else {
					valid = gtk_tree_model_iter_next(model, &iter);
				}
			}
		} else {
			gtk_show_info(global_window, "%s", "退订失败，请重试", "退票提示");
		}
	}

	EmptyTheList(&orderlist);
}

//处理退票cellrenderertoggle的选择事件
void cellrenderertoggle_refund_ticket_isrefund_toggled_cb(
		GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data) {
	GtkTreeModel *model = (GtkTreeModel *) user_data;
	GtkTreeIter iter;
	GtkTreePath *current_path = gtk_tree_path_new_from_string(path);
	gboolean isrefund;

	/* get toggled iter */
	gtk_tree_model_get_iter(model, &iter, current_path);
	gtk_tree_model_get(model, &iter, COL_IS_REFUND, &isrefund, -1);

	/* do something with the value */
	isrefund ^= 1;

	/* set new value */
	gtk_list_store_set(GTK_LIST_STORE (model), &iter, COL_IS_REFUND, isrefund,
			-1);

	/* clean up */
	gtk_tree_path_free(current_path);
}
